﻿using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using Jinndev.UI;
using System.IO;

namespace Jinndev {

    public class ChooseMapUI : BaseUIBehaviour {

        #region static
        public static readonly string PATH = "Prefab/UI/MapEditor/ChooseMapUI";

        public static ChooseMapUI Create() {
            return UIManager.Instance.Add<ChooseMapUI>(PATH, UIParam.ModalBlockMask);
        }
        #endregion


        public RectTransform textureRootSlot;
        public RectTransform mapRootSlot;

        private InputFileField inputTextureRoot;
        private InputFileField inputMapRoot;

        private void Awake() {
            inputTextureRoot = InputFileField.Create(textureRootSlot, new InputFileField.DirectoryOption("图片根目录", "存储图片的顶层目录", OnChooseTextureRoot));
            inputMapRoot = InputFileField.Create(mapRootSlot, new InputFileField.DirectoryOption("地图根目录", "打开、导出地图的顶层目录", OnChooseMapRoot));

            if (!string.IsNullOrEmpty(Setting.Instance.mapTextureRootDir)) {
                inputTextureRoot.SetPath(Setting.Instance.mapTextureRootDir, false);
            }
            if (!string.IsNullOrEmpty(Setting.Instance.mapExportRootDir)) {
                inputMapRoot.SetPath(Setting.Instance.mapExportRootDir, false);
            }
        }

        private void OnDestroy() {
            Setting.Save();
        }

        private void OnChooseTextureRoot(string path) {
            Setting.Instance.mapTextureRootDir = path;
        }

        private void OnChooseMapRoot(string path) {
            Setting.Instance.mapExportRootDir = path;
        }

        private bool IsValid() {
            if (string.IsNullOrEmpty(Setting.Instance.mapTextureRootDir) || string.IsNullOrEmpty(Setting.Instance.mapExportRootDir)) {
                DialogUI.CreateError("没有设置目录");
                return false;
            }
            if (!Directory.Exists(Setting.Instance.mapTextureRootDir)) {
                DialogUI.CreateError("图片目录不存在");
                return false;
            }
            if (!Directory.Exists(Setting.Instance.mapExportRootDir)) {
                DialogUI.CreateError("地图目录不存在");
                return false;
            }
            return true;
        }

        public void OnClickCreateMap() {
            if (!IsValid()) {
                return;
            }
            string path = Util.SaveFilePanel("选择存储位置", Setting.Instance.mapExportRootDir, "map", "json");
            if (string.IsNullOrEmpty(path)) {
                return;
            }
            if (File.Exists(path)) {
                File.Delete(path);
            }
            MapEditorUI.Create(path);
        }

        public void OnClickOpenMap() {
            if (!IsValid()) {
                return;
            }
            string path = Util.OpenFilePanel("选择地图JSON", Setting.Instance.mapExportRootDir, "json");
            if (string.IsNullOrEmpty(path)) {
                return;
            }
            MapEditorUI.Create(path);
        }

        public void OnClickTileEditor() {
            if (!IsValid()) {
                return;
            }
            TileEditorUI.Create();
        }

    }

}
